
from whiff import resolver
from whiff import gateway
from whiff.resources import PageShadow
from whiff.resources import inMemorySession
from whiff.resources import sessionDirectory
from whiff.middleware import displayTraceback
from whiff.resources import FreshName
import docroot
import os
import sys

TREE_EXAMPLE = {
	"focus":
		{ "body": "this is ME, mom<br>next line<br>third line",
		  "parent": "parent",
		  "children": ["child1", "child2"]
		},
	"parent":
		{ "body": "this is the gramma<br>next line<br>third line",
		  "children": ["sibling", "focus"]
		},
	"child1":
		{ "body": "this is the daughter<br>next line<br>third line",
		  "parent": "focus"
		},
	"child2":
		{ "body": "this is the son<br>next line<br>third line",
		  "parent": "focus"
		},
	"cousin":
		{ "body": "this is the cousin<br>next line<br>third line",
		  "parent": "sibling"
		},
	"sibling":
		{ "body": "this is the uncle<br>next line<br>third line",
		  "parent": "parent",
		  "children": ["cousin"]
		}
	}

def getApplication(edits=False, sessionDir=None, shadowDir=None):
    from docroot.navigationData import Dictionary
    environment = {
        "localTest": "http://aaron.oirt.rutgers.edu/myapp/root/",
        "treeView.textPrefix": """
        <div style="overflow:hidden"><div style="float:left">
        """,
        "treeView.textSuffix": """</div><div style="clear:left"></div></div>""",
        "treeView.leafFlag": "<div style='float:left'><img src='vartriangleright.gif' border='0'></div>",
        "treeView.expandedFlag": "<div style='float:left'><img src='circleddash.gif' border='0'></div>",
        "treeView.closedFlag": "<div style='float:left'><img src='bigoplus.gif' border='0'></div>",
        }
    if edits:
        # in safe environments, allow file editting
        environment.update({
            "EDITS": True, # add edits to the environment to enable edit forms
            })
    whiffDocumentation = resolver.moduleRootApplication("/", docroot,
                                         #exception_middleware=None,
                                         environment=environment,
                                         exception_middleware=displayTraceback.__middleware__,
                                         on_not_found=None, # show traceback (could comment)
                                         auto_reload=False,
                                         )

    # install resources in the whiffDocumentation
    whiffDocumentation.registerStaticResource(prefix="TREE_EXAMPLE",
                                              resourceValue=TREE_EXAMPLE)
    whiffDocumentation.registerStaticResource(prefix="navigationData",
                                              resourceValue=Dictionary)
    if sessionDir:
        # find session storage in directory
        sessionFinder = sessionDirectory.directorySessionFinder(directory=sessionDir, timeout=10*60) # 10 minute timeout.
    else:
        # default: use in memory sessions (won't work for multiprocess servers)
        sessionFinder = inMemorySession.memorySessionFinder(timeout=60) # 1 minute timeout
    # set up file shadow directory for storing comments on page
    if not shadowDir:
        docdir = docroot.__path__[0]
        shadowDir = docdir+"/shadowfiles"
    #pr "using shadowDir", shadowDir
    #sys.stderr.write("shadowDir = "+shadowDir+"\n")
    if not os.path.exists(shadowDir):
        #pr "making dir", shadowDir
        os.mkdir(shadowDir)
        # make shadow dir wide open... probably should be more careful about this...
        os.chmod(shadowDir, int("777", 8))
    shadowfinder = PageShadow.ShadowPageFinder(shadowDir)
    fileFinder = None
    # this one should only be enabled in secure environments!
    if edits:
        fileFinder = gateway.FilePatternFinder(docdir, True, True)
        fileFinder.addMatch("W", ".whiff")
    whiffDocumentation.registerResourceFinder(prefix="session", finder=sessionFinder)
    whiffDocumentation.registerResourceFinder(prefix="shadow", finder=shadowfinder)
    if edits:
        # ONLY IN SECURE ENVIRONMENT (LOCALHOST) ALLOWS FILE EDITS.
        whiffDocumentation.registerResourceFinder(prefix="file", finder=fileFinder)
    namefinder = FreshName.FreshNameFinder()
    whiffDocumentation.registerResourceFinder(prefix="freshName", finder=namefinder)
    # returned the configured application.
    return whiffDocumentation

if __name__=="__main__":
    import wsgiref.simple_server
    print "start page at http://localhost:8888/W.intro"
    print "serving wsgi at 8888"
    whiffDocumentation = getApplication(edits=True)
    srv = wsgiref.simple_server.make_server('localhost', 8888, whiffDocumentation)
    srv.serve_forever()
                    
